home *** CD-ROM | disk | FTP | other *** search
/ Mastering Internet Develo…oft ActiveX Technologies / Mastering Internet Development with ActiveX (1996)(Microsoft).iso / labs / lab10.3 / solution / emponly.cpp next >
C/C++ Source or Header  |  1996-07-16  |  5KB  |  162 lines

  1. // EMPONLY.CPP - Implementation file for your Internet Server
  2. //    EmpOnly ISAPI Filter
  3.  
  4. #include <afx.h>
  5. #include <afxwin.h>
  6. #include <afxisapi.h>
  7. #include "resource.h"
  8. #include "EmpOnly.h"
  9.  
  10.  
  11. ///////////////////////////////////////////////////////////////////////
  12. // The one and only CEmpOnlyFilter object
  13.  
  14. CEmpOnlyFilter theFilter;
  15.  
  16.  
  17. ///////////////////////////////////////////////////////////////////////
  18. // CEmpOnlyFilter implementation
  19.  
  20. CEmpOnlyFilter::CEmpOnlyFilter()
  21. {
  22.     //Read in employee "database" text file - Employ.dat.  
  23.     //Employ.dat must be located in Scripts subdirectory.
  24.     //Since ctor is only called when EmpOnly.dll is loaded into memory, 
  25.     //any changes to database demands that web service be restarted.
  26.  
  27.     //Lab 10.3, ex 2 - Implementing EmpOnly
  28.     CString buf;
  29.     //This next path is specific to the web server installation!
  30.     CStdioFile infile("c:\\Winnt351\\System32\\InetSrv\\Scripts\\Employ.dat", CFile::modeRead);
  31.     
  32.     while (infile.ReadString(buf))
  33.     {
  34.         strAuthUsers += buf + "; ";
  35.         buf.Empty();
  36.     }    
  37.  
  38. }
  39.  
  40. CEmpOnlyFilter::~CEmpOnlyFilter()
  41. {
  42. }
  43.  
  44. BOOL CEmpOnlyFilter::GetFilterVersion(PHTTP_FILTER_VERSION pVer)
  45. {
  46.     // Call default implementation for initialization
  47.     CHttpFilter::GetFilterVersion(pVer);
  48.  
  49.     // Clear the flags set by base class
  50.     pVer->dwFlags &= ~SF_NOTIFY_ORDER_MASK;
  51.  
  52.     // Set the flags we are interested in
  53.     pVer->dwFlags |= SF_NOTIFY_ORDER_MEDIUM | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_URL_MAP;
  54.  
  55.     // Load description string
  56.     TCHAR sz[SF_MAX_FILTER_DESC_LEN+1];
  57.     ISAPIVERIFY(::LoadString(AfxGetResourceHandle(),
  58.             IDS_FILTER, sz, SF_MAX_FILTER_DESC_LEN));
  59.     _tcscpy(pVer->lpszFilterDesc, sz);
  60.     return TRUE;
  61. }
  62.  
  63. DWORD CEmpOnlyFilter::OnUrlMap(CHttpFilterContext* pCtxt,
  64.     PHTTP_FILTER_URL_MAP pMapInfo)
  65. {
  66.     //Lab 10.3, ex 2 - Implementing EmpOnly
  67.     char pstrName[100], pstrIP[20], *pstrSearch;
  68.     DWORD dwSize;
  69.     BOOL bIsAuth = FALSE;
  70.  
  71.     //Check to see if client is requesting Employees Only page.
  72.     //Use of _strlwr is possible because NT does not differentiate on case.
  73.     _strlwr(pMapInfo->pszPhysicalPath);
  74.     pstrSearch = strstr(pMapInfo->pszPhysicalPath, "emponly.htm");
  75.     if (pstrSearch == NULL) //not asking for Employees Only page
  76.         return SF_STATUS_REQ_NEXT_NOTIFICATION;
  77.         
  78.     dwSize = sizeof(pstrName);
  79.     pCtxt->GetServerVariable("REMOTE_USER", pstrName, &dwSize);
  80.     if (dwSize > 5)  //check that is valid user (not anonymous)
  81.         bIsAuth = IsAuthUserName(pstrName);
  82.         
  83.     if (bIsAuth == FALSE) //if not authorized name, then check IP
  84.     {
  85.         dwSize = sizeof(pstrIP);
  86.         pCtxt->GetServerVariable("REMOTE_ADDR", pstrIP, &dwSize);
  87.         if(dwSize > 6) //should always have client's IP, but...
  88.             bIsAuth = IsAuthUserIP(pstrIP);
  89.     }
  90.  
  91.     if (bIsAuth == TRUE)  //if user is an authorized employee
  92.     {
  93.         return SF_STATUS_REQ_NEXT_NOTIFICATION;
  94.     }
  95.     else  //if not, then redirect to consolation page
  96.     {
  97.         strcpy(pstrSearch, "Consoltn.htm");
  98.         //preclude any other filter processing on this event
  99.         return SF_STATUS_REQ_HANDLED_NOTIFICATION;
  100.     }
  101. }
  102.  
  103. ///////////////////////////////////////////////////////////////////////
  104. // Implementation member functions section
  105.  
  106. //Helper functions IsAuthUserName and IsAuthUserIP are simple search 
  107. //routines. Note two things:
  108. // - dynamic memory is being accessed through the CString strAuthUsers, 
  109. //   no locking is necessary is necessary since it is read-only 
  110. //   (CString::Find is a constant member function).
  111. // - Routines do not guard against positive hits on substrings, such 
  112. //   as finding "ikeFen" inside "MikeFen".
  113.  
  114. //Lab 10.3, ex 2 - Implementing EmpOnly
  115. BOOL CEmpOnlyFilter::IsAuthUserName(LPCSTR lpszUserName)
  116. {
  117.     int hit = strAuthUsers.Find(lpszUserName);
  118.     if (hit == -1)
  119.         return FALSE;
  120.     else
  121.         return TRUE;
  122. }
  123.  
  124. BOOL CEmpOnlyFilter::IsAuthUserIP(LPCSTR lpszIPAddress)
  125. {
  126.     int hit = strAuthUsers.Find(lpszIPAddress);
  127.     if (hit == -1)
  128.         return FALSE;
  129.     else
  130.         return TRUE;
  131. }
  132.  
  133.  
  134. ///////////////////////////////////////////////////////////////////////
  135. // If your extension will not use MFC, you'll need this code to make
  136. // sure the extension objects can find the resource handle for the
  137. // module.  If you convert your extension to not be dependent on MFC,
  138. // remove the comments arounn the following AfxGetResourceHandle()
  139. // and DllMain() functions, as well as the g_hInstance global.
  140.  
  141. /****
  142.  
  143. static HINSTANCE g_hInstance;
  144.  
  145. HINSTANCE AFXISAPI AfxGetResourceHandle()
  146. {
  147.     return g_hInstance;
  148. }
  149.  
  150. BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason,
  151.                     LPVOID lpReserved)
  152. {
  153.     if (ulReason == DLL_PROCESS_ATTACH)
  154.     {
  155.         g_hInstance = hInst;
  156.     }
  157.  
  158.     return TRUE;
  159. }
  160.  
  161. ****/
  162.